package com.jilian.mydemo.modul.order.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.FinishImgDto;
import com.jilian.mydemo.common.dto.ProductDetailDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.my.adapter.InputServiceDataAdapter;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.NumberUtils;

import java.util.List;

/**
 * 配件
 */
public class AccessoriesAdapter extends RecyclerView.Adapter<AccessoriesAdapter.ViewHolder> {
    private Activity mContext;
    private List<ProductDetailDto> datas;
    private CustomItemClickListener listener;
    private InputServiceDataAdapter inputServiceDataAdapter;

    public AccessoriesAdapter(Activity context, List<ProductDetailDto> datas, CustomItemClickListener listener, InputServiceDataAdapter inputServiceDataAdapter) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
        this.inputServiceDataAdapter = inputServiceDataAdapter;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_accessories, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvPrice.setText("￥" + NumberUtils.forMatNumber(Double.parseDouble(datas.get(position).getActualPrice())));
        holder.tvName.setText(datas.get(position).getProductName());
        holder.tvCount.setText("X" + datas.get(position).getQuantity());
        if (EmptyUtils.isNotEmpty(datas.get(position).getProductImageList())) {
            Glide
                    .with(mContext)
                    .load(datas.get(position).getProductImageList().get(0))
                    .into(holder.ivHead);
        }

        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = new Dialog(mContext, R.style.dialogFullscreen);
                dialog.setContentView(R.layout.dialog_bottom_layout);
                Window window = dialog.getWindow();
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                layoutParams.dimAmount = 0.5f;
                window.setGravity(Gravity.BOTTOM);
                window.setAttributes(layoutParams);

                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog.show();
                dialog.findViewById(R.id.btn_dialog_bottom_del).setOnClickListener(v1 -> {
                    // TODO 删除
                    dialog.dismiss();
                    datas.remove(position);
                    notifyDataSetChanged();
                    inputServiceDataAdapter.notifyDataSetChanged();
                });
                dialog.findViewById(R.id.btn_dialog_bottom_cancel).setOnClickListener(v1 -> {
                    dialog.dismiss();

                });
            }
        });


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivHead;
        private TextView tvName;
        private TextView tvPrice;
        private TextView tvCount;
        private View mItemView;
        private TextView tvDelete;


        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            mItemView = itemView;
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvCount = (TextView) itemView.findViewById(R.id.tv_count);

            tvDelete = (TextView) itemView.findViewById(R.id.tv_delete);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });


        }

    }
}
